/*
 * @Author: szx
 * @Date: 2022-04-19 16:23:02
 * @LastEditTime: 2022-04-19 16:44:43
 * @Description:
 * @FilePath: \leetcode\800-899\821\821.js
 */
/**
 * @param {string} s
 * @param {character} c
 * @return {number[]}
 */
var shortestToChar = function (s, c) {
    const arr = [];
    for (let i = 0; i < s.length; i++) if (s[i] === c) arr.push(i);
    let i = 0;
    const res = new Array(arr.length);
    const len = arr.length;
    for (let j = 0; j < s.length; j++) {
        if (i < len - 1 && arr[i + 1] < j) i++;
        res[j] = Math.abs(j - arr[i]);
        if (len > 1 && i < len - 1) res[j] = Math.min(res[j], Math.abs(j - arr[i + 1]));
    }
    return res;
};
console.log(shortestToChar('abba', 'b'));
console.log(shortestToChar('baaa', 'b'));
console.log(shortestToChar('aaab', 'b'));
console.log(shortestToChar('abab', 'b'));
